package com.ruoyi.project.product.info.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.product.info.domain.Info;
import com.ruoyi.project.product.info.mapper.InfoMapper;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.series.mapper.SeriesMapper;
import com.ruoyi.project.product.size.domain.Size;
import com.ruoyi.project.product.size.mapper.SizeMapper;
import com.ruoyi.project.product.style.domain.Style;
import com.ruoyi.project.product.style.mapper.StyleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/16
 * @Description: com.ruoyi.project.product.info.service
 * @version: 1.0
 */
@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    private SeriesMapper seriesMapper;

    @Autowired
    private StyleMapper styleMapper;

    @Autowired
    private SizeMapper sizeMapper;

    @Autowired
    private InfoMapper infoMapper;

    /**
     * 获取产品系列
     * @return
     */
    @Override
    public List<Series> getSeries() {
        return seriesMapper.selectSeries();
    }

    /**
     * 获取产品风格
     * @return
     */
    @Override
    public List<Style> getStyle() {
        return styleMapper.selectStyle();
    }

    /**
     * 获取产品规格
     * @return
     */
    @Override
    public List<Size> getSize() {
        return sizeMapper.selectSize();
    }

    /**
     * 获取产品信息列表
     * @param info
     * @return
     */
    @Override
    public List<Info> selectInfoList(Info info) {
        return infoMapper.selectInfoList(info);
    }

    @Override
    public String checkInfoNameUnique(Info info) {
        Long infoId = StringUtils.isNull(info.getInfoId()) ? -1L : info.getInfoId();
        Info information = infoMapper.checkInfoNameUnique(info.getInfoName());
        if (StringUtils.isNotNull(information) && information.getInfoId().longValue() != infoId.longValue())
        {
            return UserConstants.SERIES_NAME_NOT_UNIQUE;
        }
        return UserConstants.SERIES_NAME_UNIQUE;
    }

    @Override
    public int insertInfo(Info info) {
        info.setCreateBy(ShiroUtils.getLoginName());
        return infoMapper.insertInfo(info);
    }

    @Override
    public Info selectInfoById(Long infoId) {
        return infoMapper.selectInfoById(infoId);
    }

    @Override
    public int updateInfo(Info info) {
        return infoMapper.updateInfo(info);
    }

    @Override
    public int deleteInfoByIds(String ids) {
        Long[] infoIds = Convert.toLongArray(ids);
        //应该在删除之前先校验该类型下是否有公告，有则不能删除，报错

        return infoMapper.deleteInfoByIds(infoIds);
    }
}
